package com.lj.luoye.lotteryticket.analysis;

import com.lj.luoye.lotteryticket.dao.LotteryBuyDao;
import com.lj.luoye.lotteryticket.dao.LotteryBuyMultiDao;
import com.lj.luoye.lotteryticket.dao.LotteryHistoryDao;
import com.lj.luoye.lotteryticket.entity.LuckEntity;
import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class WinningAnalyzer {

    private LotteryHistoryDao lotteryHistoryDao;
    private UnionlottoFromNetGetter unionlottoFromNetGetter;
    private LotteryBuyDao lotteryBuyDao;
    private LotteryBuyMultiDao lotteryBuyMultiDao;

    public WinningAnalyzer(LotteryHistoryDao lotteryHistoryDao,String url,LotteryBuyDao lotteryBuyDao,LotteryBuyMultiDao lotteryBuyMultiDao) {
        this.lotteryHistoryDao=lotteryHistoryDao;
        this.unionlottoFromNetGetter=new UnionlottoFromNetGetter(url);
        this.lotteryBuyDao=lotteryBuyDao;
        this.lotteryBuyMultiDao=lotteryBuyMultiDao;
    }

    void printUnionlotto(Unionlotto unionlotto){
        //输出结果
        System.out.print(unionlotto.getDate()+":");
        for(String redBall:unionlotto.getRedBalls()){
            System.out.print(redBall+" ");
        }
        System.out.println("- "+unionlotto.getBlueBall());
    }

    private void insertToStore(List<Unionlotto> unionlottoList){
        for(Unionlotto unionlotto:unionlottoList){
            this.lotteryHistoryDao.insert(unionlotto);
        }
    }

    //
    public void analyze(CheckCallback checkCallback,String lotteryDate){
        //从文件获取
        List<Unionlotto> rightUnionlottoList=this.lotteryHistoryDao.getAll();
        Collections.sort(rightUnionlottoList);
        Unionlotto latest=null;
        if(rightUnionlottoList.size()>0){
            latest=rightUnionlottoList.get(0);
        }
        System.out.print("最近一期:");
        if(latest!=null) {
            printUnionlotto(latest);
        }
        //从网络获取
        List<Unionlotto> unionlottoListTemp=this.unionlottoFromNetGetter.get(latest);
        if(latest==null){
            rightUnionlottoList=unionlottoListTemp;
            insertToStore(unionlottoListTemp);
        }else {
            List<Unionlotto> unionlottoListBase = new ArrayList<Unionlotto>();
            for (Unionlotto unionlotto : unionlottoListTemp) {
                if (unionlotto.getDate().equals(latest.getDate())) {
                    break;
                }
                unionlottoListBase.add(unionlotto);
            }
            insertToStore(unionlottoListBase);
            unionlottoListBase.addAll(rightUnionlottoList);
            rightUnionlottoList = unionlottoListBase;
        }
        if(rightUnionlottoList.size()>0) {
            Unionlotto right=null;
            for(Unionlotto unionlotto:rightUnionlottoList){
                if(unionlotto.date.equals(lotteryDate)){
                    right=unionlotto;
                    break;
                }
            }
            if(right!=null){
                //从存储中获取上次购买的
                List<Unionlotto> buys=lotteryBuyDao.getByDate(lotteryDate);
                if(buys.size()>0) {
                    LuckFunc luckFunc = new LuckFunc();
                    List<LuckEntity> luckEntityList = luckFunc.startlotto(buys, right);
                    //打印中奖结果
                    System.out.println("\n\n" + lotteryDate + ":");
                    System.out.print("开奖号码:");
                    printUnionlotto(right);
                    System.out.println("");
                    int i = 0;
                    for (LuckEntity luckEntity : luckEntityList) {
                        System.out.print("购买号码:");
                        printUnionlotto(buys.get(i));
                        System.out.print("中奖号码:");
                        printUnionlotto(luckEntity.unionlotto);
                        System.out.println("中奖结果：" + luckEntity.result + "\n");
                        i++;
                    }
                    //结果写回数据库
                    for (Unionlotto buy : buys) {
                        lotteryBuyDao.updateLuckResult(buy.getId(), buy.luckResult);
                    }
                    checkCallback.callBack(new CheckCallback.Result(true,0,"成功",right,buys,luckEntityList));
                }else{
                    List<Unionlotto> buysMulti=lotteryBuyMultiDao.getByDate(lotteryDate);
                    LuckFunc luckFunc = new LuckFunc();
                    List<LuckEntity> luckEntityList = luckFunc.startlottoMulti(buysMulti, right);
                    //结果写回数据库
                    for (Unionlotto buy : buysMulti) {
                        lotteryBuyMultiDao.updateLuckResult(buy.getId(), buy.luckResult);
                    }
                    checkCallback.callBack(new CheckCallback.Result(true,0,"成功",right,buysMulti,luckEntityList));
                }
            }else{
                System.out.println("还没有开奖");
                checkCallback.callBack(new CheckCallback.Result(false,-1,"还没有开奖"));
            }
        }else{
            System.out.println("没有取到开奖结果");
            checkCallback.callBack(new CheckCallback.Result(false,-1,"没有取到开奖结果"));
        }
    }
}
